package com.dgland.chatgpt.utils;

import com.theokanning.openai.OpenAiService;
import com.theokanning.openai.completion.CompletionChoice;
import com.theokanning.openai.completion.CompletionRequest;
import org.springframework.util.StringUtils;

import java.util.*;


public class OpenAiUtils {

    public static String OPENAPI_TOKEN = "";
    public static Integer TIMEOUT = null;

    /**
     * 获取ai
     *
     * @param prompt
     * @return
     */
    public static List<CompletionChoice> getAiResult(String prompt) {
        if (TIMEOUT == null ) {
            TIMEOUT = 30;
        }
        OpenAiService service = new OpenAiService(OPENAPI_TOKEN, TIMEOUT);
        CompletionRequest.CompletionRequestBuilder builder = CompletionRequest.builder()
                .model("text-davinci-003")
                .prompt(prompt)
                .temperature(0.0)
                .maxTokens(1000)
                .topP(1.0)
                .frequencyPenalty(1.0)
                .presencePenalty(0.0);
        CompletionRequest completionRequest = builder.build();
        return service.createCompletion(completionRequest).getChoices();
    }
    
}
